package com.df.ez_game.server.broker;

import com.df.ez_game.server.BrokerInfo;
import com.df.ez_game.server.BrokerManager;
import com.df.ez_game.server.NodeType;
import com.df.ez_game.server.ServerNode;

/**
 * Broker服务节点实现
 * TODO 消息队列，事件监听与发布，分布式定时任务调度(xxl-job)
 */
public class BrokerServerNode extends ServerNode {
    private BrokerManager brokerManager;
    private BrokerInfo brokerInfo;
    // 心跳发送间隔（毫秒）
    private static final long HEARTBEAT_INTERVAL = 3000;

    public BrokerServerNode(String ip, int port) {
        setType(NodeType.MsgBus);
        this.brokerInfo = new BrokerInfo(ip, port);
    }

    @Override
    public void start() {
        // 初始化BrokerManager
        brokerManager = BrokerManager.getInstance();
        // 注册到BrokerManager
        registerToBrokerManager();
        // 启动心跳定时任务
        startHeartbeatTask();
    }

    @Override
    public void stop() {
        // 停止心跳任务
        // 注销节点
    }

    @Override
    public void registerToBrokerManager() {
        brokerManager.registerBroker(brokerInfo);
    }

    @Override
    public void sendHeartbeat() {
        brokerManager.processHeartbeat(brokerInfo);
    }

    private void startHeartbeatTask() {
//        ThreadHelper.scheduleAtFixedRate(() -> {
//            sendHeartbeat();
//        }, HEARTBEAT_INTERVAL, HEARTBEAT_INTERVAL, TimeUnit.MILLISECONDS);
    }
}
